/**
 * 
 */
package sg.edu.nus.iss.vmcs.system;

import java.util.ArrayList;
import java.util.Observable;

import sg.edu.nus.iss.vmcs.util.Log;

/**
 * @author Khaing Myat Thwe - HT082208L
 *
 */
/**
 * Fault Registration
 */
public class Fault extends Observable {
	private static Fault thisObj = null;
	private FaultType state = FaultType.NO_FAULT;
	private ArrayList<FaultType> lsFaults = null;

	private Fault() {
		state = FaultType.NO_FAULT;
		lsFaults = new ArrayList<FaultType>();
	}

	public static Fault getInstance() {
		if (thisObj == null) {
			synchronized (Fault.class) {
				if (thisObj == null) {
					thisObj = new Fault();
				}
			}
		}
		return thisObj;
	}

	public void setState(FaultType faultType) {
		state = faultType;
		Log.LogIt("Fault.setState(" + state + "),");
		this.setChanged();
		if (faultType != FaultType.NO_FAULT) {
			if (!lsFaults.contains(faultType)) {
				lsFaults.add(faultType);
			}
		}
		notifyObservers(state);
	}

	public void clearFault(FaultType faultType) {
		if (lsFaults.contains(faultType)) {
			lsFaults.remove(faultType);
		}
		if (lsFaults.size() < 1) {
			setState(FaultType.NO_FAULT);
		}else{
			
		}
	}
	
	public boolean hasFault(FaultType faultType){
		return lsFaults.contains(faultType);
		
	}

	public FaultType getState() {
		return state;
	}
}
